<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width" />
    <title>Page IFrame</title>
    <style contenteditable="">
      body {
        background-color: aliceblue;
        font-family: monospace;
      }
    </style>
    <script>
      document.designMode = "on"
      document.addEventListener("readystatechange", () => {
        switch (document.readyState) {
          case "loading":
            console.log("loading");
            break;
          case "interactive":
            console.log("interactive");
            break;
          case "complete":
            console.log("complete");
            break;
          default:
            console.log("default", document.readyState);
            break;
        }
      });
    </script>
  </head>

  <body>
    <p class="output">iframe.html (iframe body)</p>
    <script>
      document.addEventListener("DOMContentLoaded", onDOMContentLoaded, {
        once: true,
      });
      function onDOMContentLoaded() {
        console.log("DOMContentLoaded");
      }
    </script>
    <script>
      const output = document.querySelector(".output");

      window.addEventListener("message", onMessage);

      function onMessage(e) {
        if (!e.ports.length) return;

        output.innerText = e.data;

        // Use the transferred port to post a message to the main frame
        // 这里的port 只有1个
        e.ports[0].postMessage("A message from the iframe in page2.html");
      }
    </script>
  </body>
</html>
